const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpackbunld=require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const devMode = process.env.NODE_ENV === 'development';


// console.log('devMode', devMode);


let HtmlWebpackPluginOption = {
    index: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/index.html',
        hash: true,
        chunks: ['vendor', 'index'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    wechat: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechat.html',
        hash: true,
        chunks: ['vendor', 'wechat'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    wechatStudent: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechatStudent.html',
        hash: true,
        chunks: ['vendor', 'wechatStudent'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    patrolpush: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/patrolpush.html',
        hash: true,
        chunks: ['vendor', 'patrolpush'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    inspectpush: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/inspectpush.html',
        hash: true,
        chunks: ['vendor', 'inspectpush'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    dangerpush: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/dangerpush.html',
        hash: true,
        chunks: ['vendor', 'dangerpush'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    scorerankpushforcounty: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/scorerankpushforcounty.html',
        hash: true,
        chunks: ['vendor', 'scorerankpushforcounty'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    scorerankpushforcity: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/scorerankpushforcity.html',
        hash: true,
        chunks: ['vendor', 'scorerankpushforcity'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    wechatmyhistory: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechatmyhistory.html',
        hash: true,
        chunks: ['vendor', 'wechatmyhistory'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    electricalsafety: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/electricalsafety.html',
        hash: true,
        chunks: ['vendor', 'electricalsafety'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }
    }),
    wechatsmokedevice: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechatsmokedevice.html',
        hash: true,
        chunks: ['vendor', 'wechatsmokedevice'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    wechatupanddowndevice: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechatupanddowndevice.html',
        hash: true,
        chunks: ['vendor', 'wechatupanddowndevice'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    dutyweekstatistics: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/dutyweekstatistics.html',
        hash: true,
        chunks: ['vendor', 'dutyweekstatistics'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    wechatsecurityrank: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechatsecurityrank.html',
        hash: true,
        chunks: ['vendor', 'wechatsecurityrank'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    wechatdepartmentrank: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechatdepartmentrank.html',
        hash: true,
        chunks: ['vendor', 'wechatdepartmentrank'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    wechatprogressrequiry: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechatprogressrequiry.html',
        hash: true,
        chunks: ['vendor', 'wechatprogressrequiry'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    wechattaskrequiry: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/wechattaskrequiry.html',
        hash: true,
        chunks: ['vendor', 'wechattaskrequiry'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    bigScreen: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/bigScreen.html',
        hash: true,
        chunks: ['vendor', 'bigScreen'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    studentinoutpush: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/studentinoutpush.html',
        hash: true,
        chunks: ['vendor', 'studentinoutpush'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    eventpush: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/eventpush.html',
        hash: true,
        chunks: ['vendor', 'eventpush'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
    kaoqinpush: new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './app/html/kaoqinpushdetail.html',
        hash: true,
        chunks: ['vendor', 'kaoqinpush'],
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        },
        chunksSortMode: 'none'
    }),
};

let entryOption = {
    index: ['./app/js/index.js'],
    wechat: ['./app/js/wechat.js'],
    wechatStudent: ['./app/js/wechatStudent.js'],
    patrolpush: ['./app/js/patrolpush.js'],
    inspectpush: ['./app/js/inspectpush.js'],
    scorerankpushforcounty: ['./app/js/scorerankpushforcounty.js'],
    scorerankpushforcity: ['./app/js/scorerankpushforcity.js'],
    dangerpush: ['./app/js/dangerpush.js'],
    wechatmyhistory: ['./app/js/wechatmyhistory.js'],
    electricalsafety: ['./app/js/electricalsafety.js'],
    wechatsmokedevice: ['./app/js/wechatsmokedevice.js'],
    wechatupanddowndevice: ['./app/js/wechatupanddowndevice.js'],
    wechatsecurityrank: ['./app/js/wechatsecurityrank.js'],
    wechatdepartmentrank: ['./app/js/wechatdepartmentrank.js'],
    wechatprogressrequiry: ['./app/js/wechatprogressrequiry.js'],
    wechattaskrequiry: ['./app/js/wechattaskrequiry.js'],
    bigScreen: ['./app/js/bigScreen.js'],
    studentinoutpush: ['./app/js/studentinoutpush.js'],
    eventpush: ['./app/js/eventpush.js'],
    kaoqinpush:['./app/js/kaoqinpushdetail.js'],
    dutyweekstatistics:['./app/js/dutyweekstatistics.js']

}

//1.选择入口
 let entryString = 'index';
// let entryString = 'wechat';
//  let entryString = 'wechatStudent';
// let entryString = 'patrolpush';
// let entryString = 'inspectpush';
 //let entryString = 'dangerpush';
//   let entryString = 'scorerankpushforcounty';
//   let entryString = 'scorerankpushforcity';
// let entryString = 'wechatmyhistory';
// let entryString = 'electricalsafety';
// let entryString = 'wechatsmokedevice';
 //let entryString = 'wechatupanddowndevice';
//  let entryString = 'wechatsecurityrank';
//  let entryString = 'wechatdepartmentrank';
//  let entryString = 'wechatprogressrequiry';
//  let entryString = 'wechattaskrequiry';
// let entryString = 'bigScreen';
// let entryString = 'studentinoutpush';
//  let entryString = 'eventpush';
// let entryString = 'kaoqinpush';
//let entryString= 'dutyweekstatistics';



const config = {
    mode: 'none',
    // devtool: 'eval-source-map',
    devtool: false,
    externals: {
       
        // "echarts":"echarts",
        "AMap":"AMap",
        "Loca":"Loca",
        "AMapUI":'AMapUI'
      　　},
    entry: {
        index: ['./app/js/index.js'],
        // wechatStudent:['./app/js/wechatStudent.js'],
        // wechat: ['./app/js/wechat.js'],
        // patrolpush: ['./app/js/patrolpush.js'],
        // studentinoutpush: ['./app/js/studentinoutpush.js'],
        // inspectpush: ['./app/js/inspectpush.js'],
        // dangerpush: ['./app/js/dangerpush.js'],
        // scorerankpushforcounty: ['./app/js/scorerankpushforcounty.js'],
        // scorerankpushforcity: ['./app/js/scorerankpushforcity.js'],
        // wechatmyhistory: ['./app/js/wechatmyhistory.js'],
        // electricalsafety: ['./app/js/electricalsafety.js'],
        // wechatsmokedevice: ['./app/js/wechatsmokedevice.js'],
        //wechatupanddowndevice: ['./app/js/wechatupanddowndevice.js'],
        // wechatsecurityrank: ['./app/js/wechatsecurityrank.js'],
        // wechatdepartmentrank: ['./app/js/wechatdepartmentrank.js'],
        // wechatprogressrequiry: ['./app/js/wechatprogressrequiry.js'],
        // wechattaskrequiry: ['./app/js/wechattaskrequiry.js'],
        //dutyweekstatistics:['./app/js/dutyweekstatistics.js'],
        // bigScreen: ['./app/js/bigScreen.js'],
        // eventpush: ['./app/js/eventpush.js'],
        //  wechatStudent: ['./app/js/wechatStudent.js'],
        // kaoqinpush: ['./app/js/kaoqinpushdetail.js'],
        
        vendor: ['react', 'react-dom', 'react-router']
    }, //入口文件
    output: { //输出目录
        path: __dirname + "/dist", //打包后的js文件存放的地方
        filename: devMode ? "js/[name].js" : "js/[name].[chunkhash].js", //打包后的js文件名
        publicPath: (!devMode && entryString !== 'index') ? "./" : "/" //打包发布版本(除index外)时这里要改成 ./
    },

    //热更新 本地虚开一个express服务器
    devServer: {
        openPage: '',
        open: true,
        inline: true,
        hot: true,
        compress: true,
        overlay: true,
        // host:'192.168.50.1',
        port: 3000,
        historyApiFallback: true,
        // proxy: {
        //     '/api': 'http://39.98.73.72:8080'
        // }
        proxy: {
            "/api": {
            target: "http://localhost:7070",
            pathRewrite:{
                "^/api":""
            }
        }
    }
    },
    module: {
        rules: [{
            test: /\.(jsx|js)$/,
            use: [{
                loader: 'babel-loader?presets[]=react,presets[]=es2015,presets[]=stage-0',
                query: {
                    presets: ['react', 'es2015', 'stage-0'],
                    plugins: [
                        ['import', {
                            "libraryName": "antd",
                            "style": "css"
                        }],
                        ["import", {
                            libraryName: "antd-mobile",
                            style: "css"
                        }]
                    ]
                }
            }, {
                loader: 'eslint-loader'
            }],
            exclude: '/node_modules/'
        }, {
            test: /\.css$/,
            // loader: 'style-loader!css-loader',
            use: [{
                    loader: MiniCssExtractPlugin.loader,
                    options: {
                        // you can specify a publicPath here
                        // by default it uses publicPath in webpackOptions.output
                        publicPath: '../',
                        hmr: process.env.NODE_ENV === 'development',
                    },
                },
                'css-loader',
            ],
            exclude: '/node_modules/',
        }, {
            test: /\.(png|gif|jpg|jpeg|bmp|jfif)$/,
            use: {
                loader: 'url-loader?limit=8192&name=img/[name].[ext]'
            }
        },
    	{
			test: /\.(woff|woff2|eot|ttf|otf|TTF)$/,
			use: ["file-loader"],
		},]
    },
    optimization: {
        minimizer: [new UglifyJsPlugin({
            parallel: 4,
            uglifyOptions: {
                warnings: false,
                compress: {
                    drop_debugger: false, //打包正式版需要改成true
                    drop_console: false //打包正式版需要改成true
                },
                output: {
                    comments: false,
                }
            }
        })],
        // runtimeChunk: {
        //     name: "runtime"
        // },
        splitChunks: {
            cacheGroups: {
                vendor: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendor",
                    priority: -20,
                    chunks: "all"
                }
            }
        },
    },
    plugins: [
        new webpackbunld(),
        new CleanWebpackPlugin(['dist'], {
            root: '', // An absolute path for the root  of webpack.config.js
            verbose: true, // Write logs to console.
            dry: false // Do not delete anything, good for testing.
        }),

        devMode ? new MiniCssExtractPlugin({
            // 类似 webpackOptions.output里面的配置 可以忽略
            filename: 'css/[name].css',
            chunkFilename: 'css/[id].css',
        }) : new MiniCssExtractPlugin({
            // 类似 webpackOptions.output里面的配置 可以忽略
            filename: 'css/[name].[chunkhash].css',
            chunkFilename: 'css/[id].[chunkhash].css',
        }),
        

        HtmlWebpackPluginOption[entryString],

        //打包MP3

        // new CopyWebpackPlugin([{
        //     from: path.resolve(__dirname, 'app/media'),
        //     to: path.resolve(__dirname, 'app/dist/media')
        //     }])
            
// ,            
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"production"' //给process加入一个属性 判断当前是开发or生产环境  在命令里设置NODE_ENV值
        }),
        // new webpack.optimize.CommonsChunkPlugin({
        //     name: 'vendor',
        //     filename: 'js/vendor.js'
        // }),
    ]
};

module.exports = function(env, argv) {
    // console.log('argv.mode', argv.mode);

    config.entry[entryString] = entryOption[entryString];

    if (argv.mode === "production") {
        //do something

    } else if (argv.mode === 'development') {
        //do something
        config.plugins.push(new webpack.HotModuleReplacementPlugin());
    }

    return config;
}